package com.twu.biblioteca.controllers;

import com.twu.biblioteca.db.Database;
import com.twu.biblioteca.exceptions.UnavailableBookException;
import com.twu.biblioteca.models.Book;

public class BookController {
    private Database database;

    public BookController(Database database) {
        this.database = database;
    }

    public String reserveBook(int id) {
        Book book = database.getBookById(id);
        String message;
        
        try {
            book.reserve();
            message = "Thank You! Enjoy the book.";
        } catch (UnavailableBookException e) {
            message = e.getMessage();
        }
        return message;
    }

    public String listBooks() {
        String listedBooks = "";
        String availabilityMessage;

        for (Book book : database.getAllBooks()) {
            availabilityMessage = book.isAvailable() ? "available" : "unavailable";
            listedBooks += book.getId() + " - " + book.getTitle() + " - " + availabilityMessage + "\n";
        }
        return listedBooks;
    }
}
